package biostienda.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import biostienda.excepcion.DAOExcepcion;
import biostienda.modelo.Categoria;
import biostienda.modelo.Transaccion;
import biostienda.util.ConexionBD;

public class TransaccionDAO extends BaseDAO{
	
/// validar el insertar para tabla transaccion y transaccion detalle
	public Transaccion insertar(Transaccion vo) throws DAOExcepcion {
		String query = "insert into Transaccion(NomTransaccion, Publicacion_idPublicacion) values (?, ?)";
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, vo.getNomTransaccion());
			stmt.setInt(2, vo.getIdPublicacion().getIdPublicacion());
			
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo insertar");
			}
			// Obtener el ultimo id
			int id = 0;
			query = "select last_insert_id()";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			if (rs.next()) {
				id = rs.getInt(1);
			}
			vo.setIdTransaccion(id);
			
			
			
			///////////////////////////////////////////
			
			
			query = "insert into transacciondetalle( Persona_idPersona,Transaccion_idTransaccion,Cantidad, Comentario) values (?,?,?,?)";
			stmt = con.prepareStatement(query);
			
			stmt.setInt(1, vo.getIdPersona().getPersona());
			stmt.setInt(2, vo.getIdTransaccion());
			stmt.setInt(3, vo.getCantidad());
			stmt.setString(4, vo.getComentario());
			
			int i2 = stmt.executeUpdate();
			if (i2 != 1) {
				throw new SQLException("No se pudo insertar");
			}
			
			
			
			
			
			

		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}


	public Collection<Transaccion> listar() throws DAOExcepcion {
		Collection<Transaccion> c = new ArrayList<Transaccion>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			String query = "select * from Transaccion order by nomTransaccion";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			while (rs.next()) {
				Transaccion vo = new Transaccion();
				//vo.setIdCategoria(rs.getInt("idTransaccion"));
				///vo.setNombre(rs.getString("nomTransaccion"));
				//vo.setDescripcion(rs.getString("descripcion"));
				c.add(vo);
			}

		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return c;
	}

	

}
